<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0,
            maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>webssh</title>
        <link rel="icon" href="./favicon.ico" />
        <link href="./lib/bootstrap/3.3.7/bootstrap.min.css" rel="stylesheet">
        <link href="./lib/xterm/4.8.1/xterm.css" rel="stylesheet">
        <link href="./lib/toastr/2.1.4.7/toastr.min.css" rel="stylesheet">
        <link href="./js/keyboard/expand-keyboard.css" rel="stylesheet">
        <style>
        .input-width {
            width: 20%;
            display: inline-block;
        }

        .hide {
            display: none;
        }
        /* body{
            -webkit-touch-callout: all;
            -webkit-user-select: all;
            -moz-user-select: all;
            -ms-user-select: all;
            user-select: all;
        } */
    </style>
    </head>
    <body>
        <h3>Welcome to XCloud WebConsole !</h3>

        <button onclick="openAdd()">ADD</button>
        <button onclick="openSession()">Sessions</button>

        <div id="add" hidden>
            <label for="alias">Alias:</label><input id="alias" /><br>
            <label for="address">Address:</label><input id="address" /><br>
            <label for="username">Username:</label><input id="username" /><br>
            <label><input name="type" type="radio" value="1" onchange="changeType(1)" />密码</label>
            <label><input name="type" type="radio" value="2" onchange="changeType(2)" />密钥</label>
            <div id="passwordDiv">
                <label for="password">Password:</label><input type="password" id="password" /><br>
            </div>
            <div id="sshKeyDiv">
                <label for="sshKey">SshKey:</label><textarea id="sshKey" rows="2"></textarea><input
                    id="sshKeyFile"
                    type="file" onchange="changeFile()"><br>
            </div>
            <button onclick="save()">Save</button>
        </div>
        <div id="sessions" hidden>
            <ul id="sessionsul" style="margin-block-start: 0;margin-block-end:0;">
            </ul>
        </div>
        <div id="webssh-terminal" class="hide1" style="height: 27em; background: red;">
            <div id="terminal"></div>
        </div>
        <div class="expand-keyboard-container">
            <div>
                <button id="keyboard-esc" class="expand-keyboard">Esc</button>
                <button id="keyboard-ctrl" class="expand-keyboard">Ctrl</button>
                <button id="keyboard-gang" class="expand-keyboard">-</button>
                <button id="keyboard-up" class="expand-keyboard">↑</button>
                <button id="keyboard-paste" class="expand-keyboard">Paste</button>
                <button id="keyboard-shift-left" class="expand-keyboard">Shift←</button>
            </div>
            <div>
                <button id="keyboard-tab" class="expand-keyboard">Tab</button>
                <button id="keyboard-pie" class="expand-keyboard">/</button>
                <button id="keyboard-left" class="expand-keyboard">←</button>
                <button id="keyboard-down" class="expand-keyboard">↓</button>
                <button id="keyboard-right" class="expand-keyboard">→</button>
                <button id="keyboard-shift-right" class="expand-keyboard">Shift→</button>
            </div>
        </div>

        <script type="text/javascript" src="./lib/jquery/3.4.1/jquery.min.js"></script>
        <script type="text/javascript" src="./lib/zmodem/0.1.10/zmodem.min.js"></script>
        <script type="text/javascript" src="./lib/xterm/4.8.1/xterm.js"></script>
        <script type="text/javascript" src="./lib/bootstrap/3.3.7/bootstrap.min.js"></script>
        <script type="text/javascript" src="./lib/bootbox/5.4.0/bootbox.min.js"></script>
        <script type="text/javascript" src="./lib/toastr/2.1.4.7/toastr.min.js"></script>
        <script type="text/javascript" src="./js/webssh.js"></script>
        <script type="text/javascript" src="./js/admin.js"></script>
        <script type="text/javascript" src="./js/keyboard/expand-keyboard.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js"></script>
        <script type="text/javascript">
            /**
             * 在ios中软键盘弹起时，仅会引起 $('body').scrollTop 值改变，我们可以通过输入框的获取焦点情况来做判断，
             * 在ios中采用这个方案，因为在android中存在主动收起键盘后，但输入框并没有失焦，而ios中键盘收起后就会失焦；
             * 在android中软键盘弹起或收起时，会改变window的高度，因此监听window的 onresize 事件； 
             **/
            if (navigator.userAgent.toUpperCase().indexOf("ANDROID") != -1) {
                console.info("监听键盘(Android)...")
                //获取原窗口的高度
                let originalHeight = document.documentElement.clientHeight || document.body.clientHeight
                window.οnresize = function() {
                    //键盘弹起与隐藏都会引起窗口的高度发生变化
                    let resizeHeight = document.documentElement.clientHeight || document.body.clientHeight
                    if (resizeHeight < originalHeight) {
                        alert("Android软键盘弹出的事件处理")
                    } else {
                        alert("Android软键盘收起的事件处理")
                    }
                }
            } else if(navigator.userAgent.toUpperCase().indexOf("IOS") != -1) {
                console.info("监听键盘(iOS)...")
                // focusin和focusout支持冒泡，对应focus和blur, 
                // 使用focusin和focusout的原因是focusin和focusout可以冒泡，focus和blur不会冒泡，
                // 这样就可以使用事件代理，处理多个输入框存在的情况。
                document.body.addEventListener('focusin', () => {
                    // alert("iOS软键盘弹出的事件处理"+$(document).scrollTop()+"--"+document.documentElement.clientHeight)
                    // $(".expand-keyboard-container").css({"bottom": "220px"})
                    $(document).scrollTop(10000)
                    // 如果弹出时系统键盘是英文输入法，然后使用时切换为非英文输入法，这时候占用高度会增加，
                    // 此时需增加扩展按键容器div距离底部的高度，防止被系统键盘遮住扩展键盘
                    // verifyed on iPhone6/7/8(4.8x4.8)
                    $(".expand-keyboard-container").css({"bottom": "45px"});
                    // TODO 目前，当系统键盘弹出时是中文然后切英文，与系统键盘顶部会出现一块间隙，
                    // 优化方案：动态把xterm容器向上撑起来(让其看见terminal块的底部即可)
                    // alert($(".expand-keyboard-container").css("bottom"))
                });
                document.body.addEventListener('focusout', () => {
                    // alert("iOS软键盘收起的事件处理"+$(document).scrollTop()+"--"+document.documentElement.clientHeight)
                    // $(".expand-keyboard-container").css({"bottom": "5px"})
                    $(document).scrollTop(0);
                    $(".expand-keyboard-container").css({"bottom": "1px"});
                    // alert($(".expand-keyboard-container").css("bottom"))
                });
            }
        </script>
    </body>
</html>